/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getAgree,
  getBranch,
  getCourier,
  getKop,
  getThird,
  getKopBrand,
  getCaptcha,
  searchBranch,
  auth,
  cancel,
  getCourierInfo,
  getGroup,
  getAllSource,
} from "@/services/source";
import { message } from "antd";
import { isArray } from "lodash";

export default {
  namespace: "source",

  state: {
    agree: [],
    branch: [],
    courier: [],
    kop: [],
    third: [],
    group: [],
    kopBrand: [],
    branchBrand: [],
    allSource: {},
    agreeList: [],
  },

  effects: {
    // 签约快递员列表
    *getAgree({ payload }, { call, put }) {
      const res = yield call(getAgree, payload);
      const { data } = res;
      const agree = [];
      if (isArray(data)) {
        data.map(item => {
          item.since != "0" && agree.push(item);
        });
      }
      yield put({
        type: "save",
        payload: {
          agree,
          agreeList: isArray(data) ? data : [],
        },
      });
    },
    // 77商家权列表
    *getBranch({ payload }, { call, put }) {
      const res = yield call(getBranch, payload);
      const { data } = res;
      yield put({
        type: "save",
        payload: {
          branch: (isArray(data) && data) || [],
        },
      });
    },
    // 快递员自有面单列表
    *getCourier({ payload }, { call, put }) {
      const res = yield call(getCourier, payload);
      const { data } = res;
      yield put({
        type: "save",
        payload: {
          courier: (isArray(data) && data) || [],
        },
      });
    },
    // 大客户授权列表
    *getKop({ payload }, { call, put }) {
      const res = yield call(getKop, payload);
      const { data } = res;
      yield put({
        type: "save",
        payload: {
          kop: (isArray(data) && data) || [],
        },
      });
    },
    // 获取第三方单号源
    *getThird({ payload }, { call, put }) {
      const res = yield call(getThird, payload);
      const { code, data } = res;
      const third = [];
      if (code == 0 && data) {
        Object.keys(data).map(key => {
          const item = data[key];
          third.push({
            key,
            list: (isArray(item) && item.length && item) || [],
          });
        });
      }
      yield put({
        type: "save",
        payload: {
          third,
        },
      });
    },

    // 获取团队分享单号源
    *getGroup({ payload }, { call, put }) {
      const res = yield call(getGroup, payload);
      const { data } = res;
      yield put({
        type: "save",
        payload: {
          group: (isArray(data) && data) || [],
        },
      });
    },
    // 获取所有单号源
    *getAllSource({ payload, __dva_resolve }, { call, put }) {
      const res = yield call(getAllSource, payload);
      const { data } = res;
      if (res.code == 0) {
        const third = [];
        data.third &&
          data.third.map(item => {
            const { key } = item;
            // 兼容大客户字段
            (item.source = (isArray(item.list) && item.list) || []),
              (item.user_name = item.nickName);
            item.status = 1;

            delete item.key;
            delete item.list;
            third.push({
              key,
              list: [item],
            });
          });
        if (third.length > 0) {
          data.third = third;
        }

        yield put({
          type: "save",
          payload: {
            allSource: data,
          },
        });
        __dva_resolve(res);
      } else {
        __dva_reject(res);
      }
    },

    // 大客户品牌列表
    *getKopBrand({ payload, __dva_resolve }, { call, put, select }) {
      const { kopBrand } = yield select(_ => _.source);
      if (kopBrand && kopBrand.length) {
        __dva_resolve(kopBrand);
        return;
      }
      const res = yield call(getKopBrand, payload);
      const { code, data, msg } = res;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            kopBrand: (isArray(data) && data) || [],
          },
        });
        __dva_resolve(data || []);
      } else {
        __dva_reject(msg);
      }
    },
    // 获取快递员信息
    *getCourierInfo({ payload, __dva_resolve }, { call }) {
      const res = yield call(getCourierInfo, payload);
      __dva_resolve(res);
    },
    // 快递员绑定验证码
    *getCaptcha({ payload, __dva_resolve, __dva_reject }, { call }) {
      const res = yield call(getCaptcha, payload);
      const { code, msg } = res;
      if (code == 0) {
        __dva_resolve(res);
        message.success("验证码已发送");
      } else {
        __dva_reject(msg);
      }
    },
    *searchBranch({ payload }, { call, put }) {
      const res = yield call(searchBranch, payload);
      const { data } = res;
      yield put({
        type: "save",
        payload: {
          branchBrand: (isArray(data) && data) || [],
        },
      });
    },
    *auth({ payload, kind, __dva_resolve }, { call, put }) {
      const res = yield call(auth, payload, kind);
      const { code, msg, data } = res;
      if (code == 0) {
        const keys = ["wuliuyun", "taobao", "pindd", "pdd_universal"];
        if (keys.includes(kind)) {
          if (data) {
            const { pathname, search } = window.location || {};
            let { url, result } = data;
            if (!url) {
              url = result && result.url;
            }
            if (url) {
              yield put({
                type: "global/setThirdAuth",
                payload: {
                  redirect: `${pathname}${search}`,
                  action: "pdd-source",
                },
              });
              window.location.href = url;
            }
          }
        } else {
          message.success("授权成功");
        }
      } else {
        message.error(msg);
      }
      __dva_resolve(res);
    },
    *cancel({ payload, kind, __dva_resolve }, { call }) {
      const res = yield call(cancel, payload, kind);
      const { code, msg } = res;
      if (code == 0) {
        message.success("已解除");
      } else {
        message.error(msg);
      }
      __dva_resolve(res);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
